// import { createRequire } from 'module';
// const require = createRequire(import.meta.url);
const path = require('path')
// import { fileURLToPath } from 'url';
// const __dirname = path.dirname(fileURLToPath(import.meta.url));

const { app, BrowserWindow, ipcMain } = require('electron')

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    autoHideMenuBar: true,//自动隐藏菜单档
    alwaysOnTop: true,
    webPreferences: {
      sandbox: false,
      preload: path.join(__dirname, './preload/index.cjs')
    },
  })

  // win.loadFile('index.html')

  win.loadURL('http://127.0.0.1:5173/')
  // 打开electron的开发者工具
  // win.loadFile('./index.html')
  // win.openDevTools()
  win.webContents.openDevTools({ mode: 'detach' })
}

ipcMain.handle('sent-event', (event, params) => {
  console.log(params)
  return '1111'
})

app.whenReady().then(() => {
  createWindow()
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
